#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "ClipboardPage.h"
#include "../client/SettingsManager.h"
#include "../client/ClientProfileManager.h"
#include "ParamDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

PropPage::TextItem ClipboardPage::texts[] = {
	{ IDC_ADD, ResourceManager::ADD },
	{ IDC_CHANGE, ResourceManager::SETTINGS_CHANGE },
	{ IDC_REMOVE, ResourceManager::REMOVE },
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item ClipboardPage::items[] = {
	{ IDC_NAME_ONE, SettingsManager::CLIPBOARD_NAME_ONE, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_NAME_TWO, SettingsManager::CLIPBOARD_NAME_TWO, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_NAME_THREE, SettingsManager::CLIPBOARD_NAME_THREE, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_NAME_FOUR, SettingsManager::CLIPBOARD_NAME_FOUR, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_NAME_FIVE, SettingsManager::CLIPBOARD_NAME_FIVE, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_NAME_SIX, SettingsManager::CLIPBOARD_NAME_SIX, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_TEXT_ONE, SettingsManager::CLIPBOARD_TEXT_ONE, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_TEXT_TWO, SettingsManager::CLIPBOARD_TEXT_TWO, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_TEXT_THREE, SettingsManager::CLIPBOARD_TEXT_THREE, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_TEXT_FOUR, SettingsManager::CLIPBOARD_TEXT_FOUR, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_TEXT_FIVE, SettingsManager::CLIPBOARD_TEXT_FIVE, PropPage::T_STR_SHOWDEFAULT },
	{ IDC_TEXT_SIX, SettingsManager::CLIPBOARD_TEXT_SIX, PropPage::T_STR_SHOWDEFAULT},
	{ 0, 0, PropPage::T_END }
};

LRESULT ClipboardPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	
	CRect rc;

	ctrlParams.Attach(GetDlgItem(IDC_PARAM_ITEMS));
	ctrlParams.GetClientRect(rc);

	ctrlParams.InsertColumn(0, CTSTRING(SETTINGS_NAME), LVCFMT_LEFT, rc.Width() / 3, 0);
	ctrlParams.InsertColumn(1, CTSTRING(REGEXP), LVCFMT_LEFT, (rc.Width() / 3) * 2, 1);

	if(BOOLSETTING(FULL_ROW_SELECT))
		ctrlParams.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);

	StringMap& pm = ClientProfileManager::getInstance()->getParams();
	StringList cols;
	for(StringMap::iterator j = pm.begin(); j != pm.end(); ++j) {
		cols.push_back(j->first);
		cols.push_back(j->second);
		ctrlParams.insert(cols);
		cols.clear();
	}

	PropPage::read((HWND)*this, items);
	return TRUE;
}

void ClipboardPage::write() {
#define BUFLEN 256
	int it = ctrlParams.GetItemCount();
	TCHAR buf[BUFLEN];
	string name, regexp;
	StringMap& pm = ClientProfileManager::getInstance()->getParams();
	pm.clear();
	for(int i = 0; i < it; ++i) {
		ctrlParams.GetItemText(i, 0, buf, BUFLEN);
		name = buf;
		ctrlParams.GetItemText(i, 1, buf, BUFLEN);
		regexp = buf;
		pm[name] = regexp;
	}
	ClientProfileManager::getInstance()->saveClientProfiles();
	PropPage::write((HWND)*this, items);
}

LRESULT ClipboardPage::onBtn(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	ParamDlg dlg;
	if (wID == IDC_ADD) {
		if(dlg.DoModal() == IDOK) {
			StringList lst;
			lst.push_back(dlg.name);
			lst.push_back(dlg.regexp);
			ctrlParams.insert(lst);
		}
	} else if (wID == IDC_REMOVE) {
		if(ctrlParams.GetSelectedCount() == 1) {
			ctrlParams.DeleteItem(ctrlParams.GetNextItem(-1, LVNI_SELECTED));
		}
	} else if (wID == IDC_CHANGE) {
		if(ctrlParams.GetSelectedCount() == 1) {
			int sel = ctrlParams.GetNextItem(-1, LVNI_SELECTED);
			TCHAR buf[BUFLEN];
			ParamDlg dlg;
			ctrlParams.GetItemText(sel, 0, buf, BUFLEN);
			dlg.name = buf;
			ctrlParams.GetItemText(sel, 1, buf, BUFLEN);
			dlg.regexp = buf;

			if(dlg.DoModal() == IDOK) {
				ctrlParams.SetItemText(sel, 0, dlg.name.c_str());
				ctrlParams.SetItemText(sel, 1, dlg.regexp.c_str());
			}
		}
	}
	return 0;
}